#!/usr/bin/env python
#
#       Graphics.py
#       
#       Copyright 2009 Ben Davis <its.hip@gmail.com>, Pete Lord <mr.petelord@gmail.com>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import os
import pygame

from Client import Client
client = Client()

class Graphics(dict):
    
    def __getitem__(self, path):
        try:
            # Try to return the surface(s)
            return dict.__getitem__(self, path)
        except KeyError:
            self.__setitem__(path, self.__load__(path))
            return dict.__getitem__(self, path)
    
    def __load__(self, path):
        return pygame.image.load(path)
        

class ShipGraphics(Graphics):

    def __load__(self, path):
        return self.__load_image_set__(path, 256), self.__load_image_set__(path, 128), self.__load_image_set__(path, 64)#, self.__load_icon__(path)
        
        
    def __load_image_set__(self, path, size):
        try:
            surface = pygame.image.load(os.path.join(path, str(size) + '.png'))
        except pygame.error:
            surface = pygame.image.load(os.path.join(path[:-1] + '0', str(size) + '.png'))
        
        rect = pygame.Rect((0, 0), (size, size))
        return_list = [[]]
        anim_frames = surface.get_height() / size
        
        for i in range(8):
            return_list[-1].append([surface.subsurface(rect)])
            for x in range(1, anim_frames):
                rect.move_ip(0, size)
                return_list[-1][-1].append(surface.subsurface(rect))
                
            if anim_frames == 0:
                rect.move_ip(size, 0)
            else:
                rect.move_ip(size, -((anim_frames-1) * size))
            
        return_list.append([])
        
        for i in range(8):
            return_list[-1].append([surface.subsurface(rect)])
            for x in range(1, anim_frames):
                rect.move_ip(0, size)
                return_list[-1][-1].append(surface.subsurface(rect))
                
            if anim_frames == 0:
                rect.move_ip(size, 0)
            else:
                rect.move_ip(size, -((anim_frames-1) * size))
        return return_list
            
    def __load_icon__(self, path):
        try:
            return pygame.image.load(os.path.join(path, '32.png'))
        except pygame.error:
            return pygame.image.load(os.path.join(path[:-1] + '0', '32.png'))

class WeaponGraphics(ShipGraphics):

    def __load__(self, path):
        return self.__load_image_set__(path, 256), self.__load_image_set__(path, 128), self.__load_image_set__(path, 64)
            
class TileGraphics(Graphics):

    def __load__(self, path):
        return pygame.image.load(os.path.join(path, '256.png')), pygame.image.load(os.path.join(path, '128.png')), pygame.image.load(os.path.join(path, '64.png'))

class ButtonGraphics(Graphics):
    def __load__(self, path):
        return self.__load_image_set__(path)
        
    def __load_image_set__(self, path):
        surface = pygame.image.load(os.path.join("Data", "UI", path))
        
        rect = pygame.Rect((0,0), (surface.get_width()/3, surface.get_height()))
        return_list = []
        
        for i in range(3):
            return_list.append([surface.subsurface(rect)])
            rect.move_ip(surface.get_width()/3, 0)
            
        return return_list